
function init () {
  viewpager();  // 1 轮播图
  lowerRequest()  // 2 延缓请求
}

init()

// 1 轮播图
function viewpager () {
  // test
  // test2
  let viewpager = document.querySelector('.viewpager'),
    radiuBoxChidren = document.querySelector('.radiuBox').children,
    index = 0;
  for (let i = 0; i < radiuBoxChidren.length; i++) {
    radiuBoxChidren[i].addEventListener('click', function () {
      radiuBoxChidren[index].className = "";
      radiuBoxChidren[i].className = "active";
      index = i;
      changeImage(index);
    }, false)
  }
  setInterval(function () {
    // 修改小圆点样式
    radiuBoxChidren[index].className = "";
    if (index === 2) index = -1;
    radiuBoxChidren[index + 1].className = "active";
    index++;
    changeImage(index);
  }, 1500);
  function changeImage (index) {
    // 更改图片
    switch (index) {
      case 0:
        viewpager.style.left = "0";
        break;
      case 1:
        viewpager.style.left = "-100%";
        break;
      case 2:
        viewpager.style.left = "-200%";
        break;
    }
  }
}
// 2 延迟跳转
function lowerRequest () {
  // defined variable
  let aList = document.querySelectorAll('.app a'),
    canvas = document.getElementsByClassName('canvas')[0],
    port = /html\/?$/g,
    judge = /\/$/g,
    nowHref = window.location.href;

  // judge url includ index.html?
  if (nowHref.match(port)) {
    if (nowHref.match(judge)) {
      nowHref = nowHref.substring(0, nowHref.length - 11);
    } else {
      nowHref = nowHref.substring(0, nowHref.length - 10);
    }
  }

  // judge phone
  if (document.body.clientWidth < 450) {
    // for bind
    for (let i = 0; i < aList.length; i++) {
      let href = aList[i].getAttribute('href');
      if (href) {
        aList[i].onclick = function (e) {
          e = event || window.event;
          e.preventDefault();
          window.location.href = nowHref + href.substring(1);
        }
      } else {
        continue
      }
    }
    return
  }

  // for bind
  for (let i = 0; i < aList.length; i++) {
    let href = aList[i].getAttribute('href');
    if (href) {
      aList[i].onclick = function (e) {
        e = event || window.event;
        e.preventDefault();
        canvas.style.left = 0;
        setTimeout(() => {
          canvas.style.left = '100%';
        }, 500);
        setTimeout(() => {
          window.location.href = nowHref + href.substring(1);
        }, 900);
      }
    } else {
      continue
    }
  }
}
